package com.example.studentapp.money;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.studentapp.MainActivity;
import com.example.studentapp.R;
import com.example.studentapp.db.SQLiteDB;

public class DetailRecordActivity extends AppCompatActivity {
    ImageView money_detail_returnIV;
    TextView money_detail_editTV,money_detail_titleTV,detail_money_outTV,detail_money_inTV;
    TextView detail_money_clothTV,detail_money_foodTV,detail_money_houseTV,detail_money_useTV;
    TextView detail_money_walkTV,detail_money_otherTV,detail_money_getTV;
    
    int id = 0;
    String title = "";
    String out_title ="";
    SQLiteDB sqLiteDB;
    SQLiteDatabase sqLiteDatabase;
    

    @Override
    @SuppressLint("Range")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_record);
        init();

        // 获取上个页面传递的值
        Intent intent = getIntent();
        if(intent!=null){
            id = intent.getIntExtra("id",0);
            title = intent.getStringExtra("title");
            out_title=getString(R.string.money_out_o)+"("
                    +intent.getDoubleExtra("out",0.0)+")"+getString(R.string.money_yuan);
            money_detail_titleTV.setText(title);
            detail_money_outTV.setText(out_title);
            //数据的获取
            sqLiteDB = new SQLiteDB(getApplicationContext());
            sqLiteDatabase = sqLiteDB.getWritableDatabase();
            String sql = "select * from money where id="+id;
            Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
            if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                double cloth = cursor.getDouble(cursor.getColumnIndex("cloth"));
                double food = cursor.getDouble(cursor.getColumnIndex("food"));
                double house = cursor.getDouble(cursor.getColumnIndex("house"));
                double use = cursor.getDouble(cursor.getColumnIndex("use"));
                double walk = cursor.getDouble(cursor.getColumnIndex("walk"));
                double other = cursor.getDouble(cursor.getColumnIndex("other"));
                double get = cursor.getDouble(cursor.getColumnIndex("get_money"));
                detail_money_clothTV.setText(""+cloth);
                detail_money_foodTV.setText(""+food);
                detail_money_houseTV.setText(""+house);
                detail_money_useTV.setText(""+use);
                detail_money_walkTV.setText(""+walk);
                detail_money_otherTV.setText(""+other);
                detail_money_getTV.setText(""+get);
                detail_money_inTV.setText(getString(R.string.money_in_o)+"("
                        +get+")"+getString(R.string.money_yuan));
            }

        }

        money_detail_returnIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(DetailRecordActivity.this, MainActivity.class);
                intent.putExtra("index",2);
                startActivity(intent);
                finish();
            }
        });

        money_detail_editTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(DetailRecordActivity.this,EditRecordActivity.class);
                intent.putExtra("id",id);
                intent.putExtra("title",title);
                intent.putExtra("out_title",out_title);
                startActivity(intent);
                finish();
            }
        });
    }

    private void init() {
        money_detail_returnIV=findViewById(R.id.money_detail_returnIV);
        money_detail_editTV=findViewById(R.id.money_detail_editTV);
        money_detail_titleTV=findViewById(R.id.money_detail_titleTV);
        detail_money_outTV=findViewById(R.id.detail_money_outTV);
        detail_money_inTV=findViewById(R.id.detail_money_inTV);
        detail_money_clothTV=findViewById(R.id.detail_money_clothTV);

        detail_money_foodTV=findViewById(R.id.detail_money_foodTV);
        detail_money_houseTV=findViewById(R.id.detail_money_houseTV);
        detail_money_useTV=findViewById(R.id.detail_money_useTV);
        detail_money_walkTV=findViewById(R.id.detail_money_walkTV);
        detail_money_otherTV=findViewById(R.id.detail_money_otherTV);
        detail_money_getTV=findViewById(R.id.detail_money_getTV);
    }
}